package com.postsuperman.proxypool.service.ip66;

import com.postsuperman.proxypool.common.http.HttpManager;
import com.postsuperman.proxypool.parser.Ip66ProxyParser;
import com.postsuperman.proxypool.pojo.HttpProxy;
import com.postsuperman.proxypool.service.BaseHttpProxyService;
import com.postsuperman.proxypool.service.HttpProxyService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ： Lwl postserupman
 * @Description TODO
 * @Date 2019/2/27 12:06
 * @Version 1.0
 */
@Service
public class Ip66ProxyService extends BaseHttpProxyService implements HttpProxyService {

    @Override
    public List<HttpProxy> get() {

        List<HttpProxy> httpProxies = new ArrayList<>();
        try {
            total = initToal();
            while (page <= total) {
                workerGroup.execute(() -> {
                    String result = HttpManager.get(String.format("http://www.66ip.cn/%s.html", page));
                    httpProxies.addAll(new Ip66ProxyParser().parser(result));
                });
                page++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            isTheardStop();
        }
        return httpProxies;
    }

    /**
     * 自动初始化总页数
     *
     * @return
     */
    private Integer initToal() {
        int total = this.total;
        if (chkPage) {
            chkPage = false;

            String result = HttpManager.get("http://www.66ip.cn/");
            Document document = Jsoup.parse(result);
            Elements pageli = document.getElementById("PageList").children();
            if (pageli != null) {
                if (pageli.size() > 2) {
                    Element pageE = pageli.get(pageli.size() - 2);
                    total = Integer.parseInt(pageE.text());
                }
            }
        }
        return total;
    }

    public static void main(String[] args) {
        HttpProxyService httpProxyService = new Ip66ProxyService();
        List<HttpProxy> httpProxies = httpProxyService.get();
        System.out.println(httpProxies.size());
    }
}
